#!/bin/sh -e
#
# This batch file builds and preverifies the code for the demos.
# it then packages them in a JAR file appropriately.
#
NAME=JB4S
WTK_DIR=/home/emadrid/opt/WTK2.5.1
WTK_BIN_DIR=${WTK_DIR}/bin
WTK_LIB_DIR=${WTK_DIR}/lib
#LIB_DIR=../../../lib
LIB_DIR=${WTK_LIB_DIR}
CLDCAPI=${LIB_DIR}/cldcapi11.jar
MIDPAPI=${LIB_DIR}/midpapi20.jar
JSR75=${LIB_DIR}/jsr75.jar
PREVERIFY=${WTK_BIN_DIR}/preverify
ENCODING=UTF-8
PATHSEP=":"

BOOTCLASSPATH=${CLDCAPI}${PATHSEP}${MIDPAPI}${PATHSEP}${JSR75}
JAVAC=javac
JAR=jar

if [ -n "${JAVA_HOME}" ] ; then
  JAVAC=${JAVA_HOME}/bin/javac
  JAR=${JAVA_HOME}/bin/jar
fi

#
# Make possible to run this script from any directory'`
#
cd `dirname $0`

echo "Creating directories..."
rm -rf ./tmpclasses
rm -rf ./classes
mkdir -p tmpclasses
mkdir -p classes

echo "Compiling source files..."
find ./src -name "*.java" > files.src

${JAVAC} \
    -bootclasspath ${BOOTCLASSPATH} \
    -source 1.3 \
    -target 1.3 \
    -d ./tmpclasses \
    -encoding ${ENCODING} \
    -classpath ./tmpclasses \
    @files.src

echo "Preverifying class files..."

${PREVERIFY} \
    -classpath ${CLDCAPI}${PATHSEP}${MIDPAPI}${PATHSEP}./tmpclasses \
    -d ./classes \
    ./tmpclasses

echo "Jaring preverified class files..."
${JAR} cmf ./bin/MANIFEST.MF ./bin/${NAME}.jar -C ./classes .

echo "Jaring resorces ..."
if [ -d ./res ] ; then
    rm -rf ./.res
    svn export ./res ./tmpres
    ${JAR} uf ./bin/${NAME}.jar -C ./tmpres .
fi

echo "Building Emacs tags ..."
find ./src -iname "*.java" | etags --members -l java -o ./src/TAGS -

echo
echo "Don't forget to update the JAR file size in the JAD file!!!"
echo
